<?php

namespace Tbf\Bundle\QueueBundle\Tests;

class QueueManagerTest extends QueueBundleTestCase
{
    public function testWrite()
    {
        $this->setDbData(array(
            'tbf_queue'=>array()
        ));
        $this->getQueueManager()->getQueue('test1')->write('hehe');
        $data = $this->getTableData('tbf_queue');
        $this->assertEquals(1,count($data));
        $row = array_pop($data);
        $this->assertEquals('test1',$row['channel']);
        $this->assertEquals(serialize('hehe'),$row['data']);
    }
    public function testRead()
    {
        $this->setDbData(array(
            'tbf_queue'=>array(array(
                'id'=>1,
                'data'=>serialize('hehe1'),
                'channel'=>'test2'
            ))
        ));
        list($data,$err) = $this->getQueueManager()->getQueue('test2')->read();
        $this->assertEquals('hehe1',$data);
        $this->assertEquals(null,$err);
    }
    public function testReadEof()
    {
        $this->setDbData(array(
            'tbf_queue'=>array()
        ));
        list($data,$err) = $this->getQueueManager()->getQueue('test3')->read();
        $this->assertEquals(null,$data);
        $this->assertEquals('EOF',$err);
    }

    /**
     * @return \Tbf\Bundle\QueueBundle\QueueManager
     */
    public function getQueueManager()
    {
        return $this->getClientDic()->get('tbf_queue');
    }
}
